<?php
/**
 * View class
 * Templating system abstraction
 * @package shakephp
 * @subpackage core
 */
class View {
	/**
	 * @var string $file Name of the main template (most internal)
	 * @access private
	 */
	private $file = null;

	/**
	 * @var sring|array $layout One or more external layouts to be applied
	 * @access private
	 */
	private $layout = null;

	/**
	 * @var array $variables Array of variables that will be used by the view
	 * @access private
	 * Should be in the form ("variable_name" => mixed $value)
	 */
	private $variables = null;

	/**
	 * Constructor
	 * @param string $file Most internal template, without extension
	 * @param string|array $layout [optional] External layouts
	 * @param array $variables [optional] Variables used by the view
	 */
	function __construct($file, $layout = null, $variables = null) {
		$this->file = $file;
		$this->layout = $layout;
		$this->variables = $variables;
	}

	/**
	 * This function will render the view, using the layouts and variables
	 */
	function render() {
		//Prepare variables to be used in the template
		if(isset($this->variables) && is_array($this->variables))
			foreach($this->variables as $key => $value)
				eval('$'.$key.' = '.var_export($value, true).';');

		//Evaluate most internal template file
		ob_start();
		include('templates/'.$this->file.'.tpl');
		$content_for_layout = ob_get_contents();
		ob_end_clean();

        if(!isset($this->layout))
            print $content_for_layout;
        //Evaluate a single external template...
        elseif(!is_array($this->layout) && isset($this->layout))
            include('templates/'.$this->layout.'.tpl');
        //...or evaluate nested templates
        else {
            foreach(array_reverse($this->layout) as $chave => $template) {
                if($chave==(count($this->layout)-1))
                    include('templates/'.$template.'.tpl');
                else {
                    ob_start();
                    include('templates/'.$template.'.tpl');
                    $content_for_layout = ob_get_contents();
                    ob_end_clean();
                }
            }
        }
    }

    function getContents() {
            //Prepare variables to be used in the template
        if(isset($this->variables) && is_array($this->variables))
            foreach($this->variables as $key => $value)
                eval('$'.$key.' = '.var_export($value, true).';');

        //Evaluate most internal template file
        ob_start();
        include('templates/'.$this->file.'.tpl');
        $content_for_layout = ob_get_contents();
        ob_end_clean();

        //Evaluate a single external template...
        if(!is_array($this->layout) && isset($this->layout)) {
            ob_start();
            include('templates/'.$this->layout.'.tpl');
            $content = ob_get_contents();
            ob_end_clean();
            return $content;
        }

        //...or evaluate nested templates
        else {
            foreach(array_reverse($this->layout) as $chave => $template) {
                ob_start();
                include('templates/'.$template.'.tpl');
                if($chave==(count($this->layout)-1)) {
                    $content = ob_get_contents();
                    ob_end_clean();
                    return $content;
                }
                else {
                    $content_for_layout = ob_get_contents();
                    ob_end_clean();
                }
            }
        }
    }
}
?>
